package com.dgut.crm.system.web;

import java.util.Arrays;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dgut.crm.common.util.ExtAjaxResponse;
import com.dgut.crm.common.util.ExtJsonResult;
import com.dgut.crm.common.util.ExtPageable;
import com.dgut.crm.system.entity.Department;
import com.dgut.crm.system.entity.dto.DepartmentNameDTO;
import com.dgut.crm.system.entity.dto.DepartmentQueryDTO;
import com.dgut.crm.system.service.IDepartmentService;


@Controller
@RequestMapping("/department")
public class DepartmentContreller 
{
	@Autowired
	private IDepartmentService departmentService;
	
	@RequestMapping("/saveOrUpdate")
	public @ResponseBody ExtAjaxResponse saveOrUpdate(Department department) 
	{
		try {
			if(null!=department) {
				departmentService.saveOrUpdate(department);
			}
			return new ExtAjaxResponse(true,"操作成功！");
		} catch (Exception e) {
			return new ExtAjaxResponse(false,"操作失败！");
		}
		
	}
	@RequestMapping("/delete")
	public @ResponseBody ExtAjaxResponse delete(@RequestParam Long id) 
	{
		try {
			Department department =  departmentService.findOne(id);
			if(null!=department) {
				departmentService.delete(department);
			}
			return new ExtAjaxResponse(true,"操作成功！");
		} catch (Exception e) {
			return new ExtAjaxResponse(false,"操作失败！");
		}

	}
	@RequestMapping("/deleteDepartments")
	public @ResponseBody ExtAjaxResponse deletedepartments(@RequestParam() Long[] ids) 
	{
		try {
			List<Long> idLists = Arrays.asList(ids); 
			if(null!=idLists) {
				departmentService.deleteDepartments(idLists);
			}
			return new ExtAjaxResponse(true,"操作成功！");
		} catch (Exception e) {
			return new ExtAjaxResponse(false,"操作失败！");
		}

	}
	
	@RequestMapping("/findOne")
	public @ResponseBody Department findOne(@RequestParam Long id) 
	{
		Department department =  departmentService.findOne(id);
		
		return department;
	}
	
	@RequestMapping("/findPage")
	public @ResponseBody Page <Department>findPage(DepartmentQueryDTO departmentQueryDTO,ExtPageable pageable) 
	{
		Page<Department>page =  departmentService.findAll(DepartmentQueryDTO.getSpecification(departmentQueryDTO), pageable.getPageable());
		return page;
	}

	@RequestMapping("/findAll")
	public @ResponseBody ExtJsonResult <DepartmentNameDTO>findAll() 
	{
		List<DepartmentNameDTO> list =  departmentService.findNameAll();
		return new ExtJsonResult<DepartmentNameDTO>(true,"操作成功！",list);
	}
}
